//
//  JXSecondHandDropViewHandle.m
//  JinXun
//
//  Created by Apple on 2022/5/4.
//

#import "JXSecondHandDropViewHandle.h"
#import "NSArray+Sudoku.h"
#import "JXSelectViewHandle.h"

#define ToolBarView_heigh 40

@interface JXSHDropTooBar : UIView

@property (nonatomic, strong) UIButton *reSetButton;
@property (nonatomic, strong) UIButton *sureButton;
@property (nonatomic, copy) void (^resetBlock)(void);
@property (nonatomic, copy) void (^sureBlock)(void);

@end

@implementation JXSHDropTooBar

//MARK: - system

- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self test];
        [self p_configSubviews];
    }
    return self;
}

//MARK: - private

- (void)p_configSubviews
{
    [self addSubview:self.reSetButton];
    [self addSubview:self.sureButton];

    [self.reSetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(self);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(self);
        make.left.mas_equalTo(self.reSetButton.mas_right);
        make.width.mas_equalTo(self.reSetButton.mas_width);
    }];
}

//MARK: - action

- (void)resetAction
{
    if(!self.resetBlock) return;
    self.resetBlock();
}

- (void)sureAction
{
    if(!self.sureBlock) return;
    self.sureBlock();
}

//MARK: - get

- (UIButton *)reSetButton
{
    if (!_reSetButton) {
        UIButton *button = [UIButton new];
        [button setTitle:@"重置" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor JX_RedColor_5];
        [button addTarget:self action:@selector(resetAction) forControlEvents:UIControlEventTouchUpInside];
        _reSetButton = button;
    }
    return _reSetButton;
}

- (UIButton *)sureButton
{
    if (!_sureButton) {
        UIButton *button = [UIButton new];
        [button setTitle:@"确定" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor JX_RedColor_6];
        [button addTarget:self action:@selector(sureAction) forControlEvents:UIControlEventTouchUpInside];
        _sureButton = button;
    }
    return _sureButton;
}


//MARK: - test

- (void)test
{
    [self test_1];
}

- (void)test_1
{
//    self.sureButton.backgroundColor = [UIColor orangeColor];
//    self.reSetButton.backgroundColor = [UIColor greenColor];
}

@end

@interface JXSHDropListView : UIView

@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIView *scrollview_contentView;
@property(nonatomic, strong) UIView *maskView;
@property(nonatomic, strong) JXSelectViewHandle *selectViewHandle;
@property(nonatomic, strong) UITapGestureRecognizer *tap;
@property(nonatomic, strong) JXSHDropTooBar *toolBarView;
@property(nonatomic, strong) NSArray <JXSecondHandDropViewSelectModel *>*datas;
@property(nonatomic, strong) JXSecondHandDropViewSelectModel *selectModel;
@property(nonatomic, copy) void(^dismissBlock)(void);
@property(nonatomic, copy) void(^selectBlock)(JXSecondHandDropViewSelectModel *model);
@end

@implementation JXSHDropListView

- (void)dealloc
{
    NSLog(@"JXSHDropListView-dealloc");
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGRect rect = self.scrollview_contentView.frame;
    
    NSLog(@"layoutSubviews rect = %@",NSStringFromCGRect(rect));
}


- (void)showDatas:(NSArray <JXSecondHandDropViewSelectModel *>*)datas onView:(UIView *)view top:(CGFloat)top
{
    self.datas = datas;
    
//    [self test];
    [self p_configSubViews];
    [view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(view);
        make.top.mas_equalTo(view).offset(top);
        make.height.mas_equalTo(view.frame.size.height - top);
    }];
    
    [self.selectViewHandle configWithDatas:datas];
    [self p_updateScrollViewLayout];
}

- (void)dismiss
{
    [self removeFromSuperview];
    [self.scrollview_contentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
}

//MARK: - private

- (void)p_configSubViews
{
    [self addSubview:self.maskView];
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.scrollView];
    [self.contentView addSubview:self.toolBarView];
    
    [self.scrollView addSubview:self.scrollview_contentView];
    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
    }];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.contentView);
    }];
    
    [self.toolBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.scrollView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.contentView).offset(15);
        make.right.mas_equalTo(self.contentView).offset(-15);
        make.height.mas_equalTo(ToolBarView_heigh);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-30);
    }];
    
    [self.scrollview_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.scrollView).insets(UIEdgeInsetsMake(2, 2, 2, 2));
        make.width.mas_equalTo(self.scrollView.mas_width).offset(-10);
        make.height.greaterThanOrEqualTo(@0);
    }];

}

- (void)p_updateScrollViewLayout
{
    [self.scrollview_contentView layoutIfNeeded];
    [self layoutIfNeeded];
    
    CGFloat safeAreaInsetBottom = [UIApplication sharedApplication].keyWindow.safeAreaInsets.bottom;

    CGFloat contentView_height = self.scrollview_contentView.bounds.size.height;
    CGFloat self_height = self.bounds.size.height;
    CGFloat max_height = self_height - 10 - ToolBarView_heigh - safeAreaInsetBottom;
    
    if (contentView_height > max_height) {
        [self p_configScrollViewLayoutHeightEqualHeight:max_height];
    }else{
        [self p_configScrollViewLayoutHeightEqualHeight:contentView_height + 10];
    }
}

- (void)p_configScrollViewLayoutHeightEqualHeight:(CGFloat)height
{
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
}

- (void)p_reset:(JXSecondHandDropViewSelectModel *)model
{
    if(!model)return;
    if (!self.selectBlock) return;
    model.isSelect = NO;
    self.selectBlock(model);
}

- (void)p_sure:(JXSecondHandDropViewSelectModel *)model
{
    if(!model)return;
    if(!self.selectBlock) return;
    model.isSelect = YES;
    self.selectBlock(model);
}



//MARK: - action

- (void)tapAction
{
    [self dismiss];
    if(self.dismissBlock)self.dismissBlock();
}

//MARK: - get


- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [UIView new];
        _contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}

- (UIScrollView *)scrollView
{
    if (!_scrollView) {
        _scrollView = [UIScrollView new];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
    }
    return _scrollView;
}

- (UIView *)scrollview_contentView
{
    return self.selectViewHandle.view;
}

- (UIView *)maskView
{
    if (!_maskView) {
        _maskView = [UIView new];
        _maskView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.8];
        [_maskView addGestureRecognizer:self.tap];
    }
    return _maskView;
}

- (UITapGestureRecognizer *)tap
{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    }
    return _tap;
}

- (JXSHDropTooBar *)toolBarView
{
    if(!_toolBarView){
        _toolBarView = [JXSHDropTooBar new];
        _toolBarView.layer.cornerRadius = ToolBarView_heigh / 2.0;
        _toolBarView.clipsToBounds = YES;
        @WeakObj(self);
        _toolBarView.sureBlock = ^{
            @StrongObj(self);
            [self p_sure:self.selectModel];
        };
        
        _toolBarView.resetBlock = ^{
            @StrongObj(self);
            [self p_reset:self.selectModel];
        };
    }
    return _toolBarView;
}

- (JXSelectViewHandle *)selectViewHandle
{
    if(!_selectViewHandle){
        _selectViewHandle = [JXSelectViewHandle new];
        @WeakObj(self);
        _selectViewHandle.selectBlock = ^(JXSecondHandDropViewSelectModel * _Nonnull selectModel) {
            @StrongObj(self);
            self.selectModel = selectModel;
        };
    }
    return _selectViewHandle;
}
//MARK: - test

- (void)test
{
    [self test_1];
}

- (void)test_1
{
    self.backgroundColor = [UIColor orangeColor];
    self.toolBarView.backgroundColor = [UIColor redColor];
}

@end


@interface JXSHFilterItemView : UIView

@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UIImageView *iconView;
@property(nonatomic, strong) UIView *contentView;
@property(nonatomic, strong) UITapGestureRecognizer *tap;
@property(nonatomic, strong) void(^selectBlock)(void);

@end

@implementation JXSHFilterItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addGestureRecognizer:self.tap];
        [self p_configSuviews];
    }
    return self;
}

- (void)selectState
{
    self.titleLabel.textColor = [UIColor redColor];
    self.iconView.image = [UIImage imageNamed:@"down_selected"];
    self.iconView.transform = CGAffineTransformMakeRotation(M_PI);
}

- (void)normalState
{
    self.titleLabel.textColor = [UIColor blackColor];
    self.iconView.image = [UIImage imageNamed:@"down_normal"];
    self.iconView.transform = CGAffineTransformMakeRotation(0);
}


- (void)p_configSuviews
{
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.iconView];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.iconView.mas_left).offset(-3);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView).offset(0);
        make.height.mas_equalTo(5);
        make.width.mas_equalTo(5);
    }];
}

//MARK: - action

- (void)tapAction
{
    if(self.selectBlock)self.selectBlock();
}

//MARK: - get

- (UILabel *)titleLabel
{
    if(!_titleLabel){
        UILabel *label = [UILabel new];
        label.textColor = [UIColor JX_GrayColor_3];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:12.0];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UIImageView *)iconView
{
    if(!_iconView){
        _iconView = [UIImageView new];
        _iconView.image = [UIImage imageNamed:@"down_normal"];
    }
    return _iconView;
}

- (UIView *)contentView
{
    if (!_contentView) {
        _contentView = [UIView new];
    }
    return _contentView;
}

- (UITapGestureRecognizer *)tap
{
    if (!_tap) {
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    }
    return _tap;
}

//MARK: - test

- (void)test
{
//    箭头图标状态
//    down_normal
//    down_selected
}

@end



@interface JXSHFilterItemViewHandle : NSObject

@property(nonatomic, strong) JXSHFilterItemView *view;
@property(nonatomic, strong) JXSHDropListView *dropView;
@property(nonatomic, strong) JXSecondHandDropViewDataSouce *data;
@property(nonatomic, weak) UIView *onView;
/// 默认 NO
@property(nonatomic, assign) BOOL select;
@property(nonatomic, strong) void(^selectBlock)(BOOL select);
@property(nonatomic, copy) void(^sureBlock)(JXSecondHandDropViewSelectModel *model);
@property(nonatomic, strong) JXSecondHandDropViewSelectModel *selectModel;

@end


@implementation JXSHFilterItemViewHandle

- (void)dealloc
{
    NSLog(@"JXSHFilterItemViewHandle-dealloc");
}


- (instancetype)initWithData:(JXSecondHandDropViewDataSouce *)data onView:(UIView *)onView
{
    if(self = [super init]){
        self.view.titleLabel.text = data.title;
        self.onView = onView;
        // 默认为 第一个
        self.selectModel = data.selectArr.firstObject;
        _select = NO;
    }
    return self;
}

- (void)configSelectState:(BOOL)select
{
    if (select) {
        [self.view selectState];
    }else{
        [self.view normalState];
    }
}

- (void)show
{
    CGRect rect = [self.view convertRect:self.view.frame toView:self.onView];
    CGFloat top = rect.origin.y + rect.size.height + 10;
    [self.dropView showDatas:self.data.selectArr onView:self.onView top:top];
    [self configSelectState:YES];
    self.select = YES;
}

- (void)dismiss
{
    [self.dropView dismiss];
    [self configSelectState:NO];
    self.select = NO;
}

//MAKR: - private




//MARK: - get

- (JXSHFilterItemView *)view
{
    if (!_view) {
        _view = [JXSHFilterItemView new];
        @WeakObj(self);
        _view.selectBlock = ^{
            @StrongObj(self);
            self.select = !self.select;
            [self configSelectState:self.select];
            if (self.selectBlock) {
                self.selectBlock(self.select);
            }
        };
    }
    return _view;
}

- (JXSHDropListView *)dropView
{
    if (!_dropView) {
        _dropView = [JXSHDropListView new];
        @WeakObj(self);
        _dropView.dismissBlock = ^{
            @StrongObj(self);
            self.select = NO;
            [self configSelectState:NO];
        };
        
        _dropView.selectBlock = ^(JXSecondHandDropViewSelectModel *model) {
            @StrongObj(self);
            self.selectModel = model;
            if(self.sureBlock) self.sureBlock(model);
            [self dismiss];
        };
    }
    return _dropView;
}



@end


@implementation JXSecondHandDropViewDataSouce

@end



@interface JXSecondHandDropView : UIView

@property(nonatomic, weak) UIView *onView;

@property(nonatomic, strong) NSMutableArray <JXSHFilterItemViewHandle *>* itemArr;
@property(nonatomic, strong) NSArray <JXSecondHandDropViewSelectModel *>*selectModes;
@property(nonatomic, copy) void(^sureBlock)(NSArray <JXSecondHandDropViewSelectModel *>*selectModes);

@end

@implementation JXSecondHandDropView

- (void)dealloc
{
    NSLog(@"JXSecondHandDropView-dealloc");
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.layer.cornerRadius = self.bounds.size.height / 2.0;
    }];
    
}

- (void)configSubViews:(NSArray <JXSecondHandDropViewDataSouce *>*)datas
{
    NSMutableArray *mutableArr = [@[] mutableCopy];
    [datas enumerateObjectsUsingBlock:^(JXSecondHandDropViewDataSouce * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        JXSHFilterItemViewHandle *itemHandle = [[JXSHFilterItemViewHandle alloc] initWithData:obj onView:self.onView];
        itemHandle.data = obj;
        itemHandle.view.titleLabel.text = obj.title;
        itemHandle.view.backgroundColor = [UIColor JX_GrayColor_8];
        @WeakObj(self);
        @WeakObj(itemHandle);

        itemHandle.selectBlock = ^(BOOL select) {
            @StrongObj(self);
            @StrongObj(itemHandle);

            [self dismissOtherHandleShowView];
            select ? [itemHandle show] : [itemHandle dismiss];
        };
      
        itemHandle.sureBlock = ^(JXSecondHandDropViewSelectModel *model) {
            @StrongObj(self);
            [self p_upldataSelectModels];
        };
        
        [self.itemArr addObject:itemHandle];
        [self addSubview:itemHandle.view];
        [mutableArr addObject:itemHandle.view];
        
        [itemHandle.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self);
        }];
    }];
    
    [mutableArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:15 tailSpacing:15];
}

- (void)dismissOtherHandleShowView
{
    [self.itemArr enumerateObjectsUsingBlock:^(JXSHFilterItemViewHandle * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj dismiss];
        [obj configSelectState:NO];
    }];
}

//MARK: - private

- (void)p_upldataSelectModels
{
    NSMutableArray *mutableArr = [@[] mutableCopy];
    [self.itemArr enumerateObjectsUsingBlock:^(JXSHFilterItemViewHandle * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [mutableArr addObject:obj.selectModel];
    }];
    self.selectModes = [mutableArr copy];
    
    if (self.sureBlock) {
        self.sureBlock(self.selectModes);
    }
}


//MARK: - get

- (NSArray<JXSHFilterItemViewHandle *> *)itemArr
{
    if (!_itemArr) {
        _itemArr = [@[] mutableCopy];
    }
    return _itemArr;
}

@end


@interface JXSecondHandDropViewHandle()

@property(nonatomic, weak) id <JXSecondHandDropViewHandleDelegate>delegate;

@property(nonatomic, strong) JXSecondHandDropView *contentView;

@property(nonatomic, strong) NSArray <JXSecondHandDropViewDataSouce *>* datas;


@end



@implementation JXSecondHandDropViewHandle

- (void)dealloc
{
    NSLog(@"JXSecondHandDropViewHandle-dealloc");
}

- (instancetype)initWithDatas:(NSArray <JXSecondHandDropViewDataSouce *>*)datas onView:(UIView *)onView delegate:(id <JXSecondHandDropViewHandleDelegate>)delegate
{
    if (self = [super init]) {
        _delegate = delegate;
        _datas = datas;
        self.contentView.onView = onView;
//        [self test];
        [self.contentView configSubViews:datas];
    }
    return self;
}

//MARK: - get

- (UIView *)view
{
    return self.contentView;
}

- (JXSecondHandDropView *)contentView
{
    if (!_contentView) {
        _contentView = [JXSecondHandDropView new];
        @WeakObj(self);
        _contentView.sureBlock = ^(NSArray<JXSecondHandDropViewSelectModel *> *selectModes) {
            @StrongObj(self);
            if (self.delegate && [self.delegate respondsToSelector:@selector(JXSecondHandDropViewHandle:selectModelList:)]) {
                [self.delegate JXSecondHandDropViewHandle:self selectModelList:selectModes];
            }
        };
    }
    return _contentView;
}

//MARK: - test

- (void)test
{
    self.contentView.backgroundColor = [UIColor redColor];
}



@end
